package com.example.p2p.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.p2p.R;
import com.example.p2p.common.AppNetConfig;
import com.example.p2p.common.BaseFragment;
import com.example.p2p.util.UIUtils;
import com.loopj.android.http.RequestParams;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class InvestFragment extends BaseFragment {
    @Bind(R.id.iv_title_back)
    ImageView ivTitleBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @Bind(R.id.tabpage_invest)
    TabPageIndicator tabpageInvest;
    @Bind(R.id.vp_invest)
    ViewPager vpInvest;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return AppNetConfig.PRODUCT;
    }

    @Override
    protected void initData(String content) {
        //1. 加载三个不同的Fragment:ProductListFragment,ProductRecommondFragment
        initFragments();
        //2.ViewPager设置三个Fragment的显示
        MyAdapter myAdapter = new MyAdapter(getFragmentManager());
        vpInvest.setAdapter(myAdapter);
        //将TabPagerIndicator与ViewPager关联
        tabpageInvest.setViewPager(vpInvest);
        /**
         *  如果在布局中设置match parent,会出现ViewPager has not been bound错误
         *  所以要在加载完数据设置Adapter后再将其设置为VISIBLE
         */
        tabpageInvest.setVisibility(View.VISIBLE);

    }

    private List<Fragment> fragmentList = new ArrayList<>();

    private void initFragments() {
        ProductListFragement productListFragement = new ProductListFragement();
        ProductRecommondFragement productRecommondFragement = new ProductRecommondFragement();
        ProductHotFragement productHotFragement = new ProductHotFragement();
        //添加到集合中
        fragmentList.add(productListFragement);
        fragmentList.add(productRecommondFragement);
        fragmentList.add(productHotFragement);
    }

    @Override
    protected void initTitle() {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("投资");
        ivTitleSetting.setVisibility(View.GONE);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_invest;
    }


    /**
     * 提供PagerAdapter的实现
     * 如果ViewPager中加载的是Fragment,则提供的Adapter可以继承于具体的：FragmentStatePagerAdapter或FragmentPagerAdapter
     * FragmentStatePagerAdapter:适用于ViewPager中加载的Fragment过多，会根据最近最少使用算法，实现内存中Fragment的清理，避免溢出
     * FragmentPagerAdapter:适用于
     */
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            //方式一
//            if (position == 0){
//                return "全部理财";
//            }else if (position == 1){
//                return "推荐理财";
//            }else if (position == 2){
//                return "热门理财";
//            }

            //方式二：
            return UIUtils.getStringArr(R.array.invest_tab)[position];

        }
    }

}
